﻿using System;
using SteeringBehaviors;
using UnityEngine;

// Token: 0x020001DD RID: 477
internal class ChaseAction : AgentAction
{
	// Token: 0x06000A2F RID: 2607 RVA: 0x00059F4F File Offset: 0x0005814F
	public ChaseAction(IKinematic target, IKinematic self, EntityBase entity)
	{
		this.target = target;
		this.self = self;
		this.entity = entity;
	}

	// Token: 0x06000A30 RID: 2608 RVA: 0x00059F77 File Offset: 0x00058177
	public void GtsSpeedUpdate()
	{
		this.agent.movement.currentSpeedDivider = this.newGtsSpeed;
	}

	// Token: 0x06000A31 RID: 2609 RVA: 0x00059F90 File Offset: 0x00058190
	public override void StartAction()
	{
		if (this.entity == null || this.entity.isDead)
		{
			this.mComplete = true;
			return;
		}
		this.mSteering = (WaypointArrive)this.agent.movement.StartArriveBehavior(new VectorKinematic(this.GetExpectedInterceptPoint()));
		if (this.IsInDistance())
		{
			this.mComplete = true;
			return;
		}
		this.mComplete = false;
		this.agent.movement.move = true;
		this.countdown = 60;
	}

	// Token: 0x06000A32 RID: 2610 RVA: 0x0005A020 File Offset: 0x00058220
	public override void UpdateAction()
	{
		if (this.entity == null || this.entity.isDead)
		{
			this.mComplete = true;
			this.agent.movement.move = false;
			return;
		}
		if (this.IsInDistance())
		{
			return;
		}
		if (!this.ShouldExecute())
		{
			return;
		}
		Vector3 expectedInterceptPoint = this.GetExpectedInterceptPoint();
		if (expectedInterceptPoint != Vector3.zero)
		{
			if (this.mSteering == null || this.mSteering.QueueLength() == 0)
			{
				this.mSteering = (WaypointArrive)this.agent.movement.StartArriveBehavior(new VectorKinematic(expectedInterceptPoint));
			}
			else
			{
				this.mSteering.PurgeSteeringQueue();
				this.mSteering.target = new VectorKinematic(expectedInterceptPoint);
			}
			return;
		}
		this.mComplete = true;
		this.mSteering = null;
	}

	// Token: 0x06000A33 RID: 2611 RVA: 0x0005A108 File Offset: 0x00058308
	private bool IsInDistance()
	{
		this.newGtsSpeed = 1f;
		if (this.mSteering != null)
		{
			float num = this.mSteering.agent.entity.Height * this.mSteering.agent.tileWidth * 2f;
			float num2 = this.mSteering.agent.entity.Height * this.mSteering.agent.tileWidth * 8f;
			if (Vector3.Distance(this.target.position, this.self.position) < num)
			{
				this.agent.movement.move = false;
				this.mComplete = true;
				this.mSteering.PurgeSteeringQueue();
				this.mSteering = null;
				this.GtsSpeedUpdate();
				return true;
			}
			if (Vector3.Distance(this.target.position, this.self.position) < num2)
			{
				float num3 = Vector3.Distance(this.target.position, this.self.position) / num2;
				if (num3 < 0f || num3 > 1f)
				{
					Debug.Log("Targeting math ended with an incorrect range 0 <> 1");
				}
				this.newGtsSpeed = 1f + (1f - num3) * this.gtsSlowDownFactor;
			}
		}
		this.GtsSpeedUpdate();
		return false;
	}

	// Token: 0x06000A34 RID: 2612 RVA: 0x0005A258 File Offset: 0x00058458
	public override bool IsCompleted()
	{
		if (this.mComplete)
		{
			this.agent.movement.Stop();
		}
		return this.mComplete;
	}

	// Token: 0x06000A35 RID: 2613 RVA: 0x00059F3D File Offset: 0x0005813D
	public override void Interrupt()
	{
		this.agent.movement.Stop();
	}

	// Token: 0x06000A36 RID: 2614 RVA: 0x0005A27C File Offset: 0x0005847C
	private bool ShouldExecute()
	{
		if (this.agent.movement.currentSpeedDivider < 0.5f)
		{
			this.countdown = 0;
			return true;
		}
		if (this.target != null)
		{
			float num = Vector2.Distance(new Vector2(this.self.position.x, this.self.position.z), new Vector2(this.target.position.x, this.target.position.z));
			if (num < 10f)
			{
				this.countdown = 0;
				return true;
			}
		}
		if (this.countdown == 60)
		{
			this.countdown = 0;
			return true;
		}
		this.countdown++;
		return false;
	}

	// Token: 0x06000A37 RID: 2615 RVA: 0x0005A350 File Offset: 0x00058550
	private Vector3 GetExpectedInterceptPoint()
	{
		float y = this.target.position.y;
		Vector2 vector = new Vector2(this.self.position.x, this.self.position.z);
		Vector2 vector2 = new Vector2(this.target.position.x, this.target.position.z);
		Vector2 zero = Vector2.zero;
		Vector2 zero2 = Vector2.zero;
		Vector2 vector3 = InterceptionCalculator.CalculateInterceptionCourse(vector2, zero, vector, zero2);
		Vector3 result = new Vector3(vector3.x, y, vector3.y);
		return result;
	}

	// Token: 0x04000F8E RID: 3982
	private EntityBase entity;

	// Token: 0x04000F8F RID: 3983
	private IKinematic target;

	// Token: 0x04000F90 RID: 3984
	private IKinematic self;

	// Token: 0x04000F91 RID: 3985
	private bool mComplete;

	// Token: 0x04000F92 RID: 3986
	private int countdown;

	// Token: 0x04000F93 RID: 3987
	private bool mInitialCheck;

	// Token: 0x04000F94 RID: 3988
	private float gtsSlowDownFactor = 2f;

	// Token: 0x04000F95 RID: 3989
	private float newGtsSpeed;

	// Token: 0x04000F96 RID: 3990
	private WaypointArrive mSteering;

	// Token: 0x04000F97 RID: 3991
	private const int cycleTime = 60;
}
